package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public abstract class BaseStudentController {
    Scanner sc = new Scanner(System.in);
    private StudentService studentService = new StudentService();

    public final void start() {

        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();

            switch (choice) {
                case "1":
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudentById();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updataStudentInfo();
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢您使用学生管理系统，再见！");
                    return;
                default:
                    System.out.println("您的输入有误，请重新输入！");
                    break;
            }
        }
    }

    //修改学生方法
    public final void updataStudentInfo() {
        //调用底层方法，判断所修改ID是否存在
        String updataId = studentInfo();
        //若存在,则调用底层方法进行修改
        Student student = inputStudentInfo(updataId);
        studentService.updataStudentInfo(student, updataId);
        System.out.println("修改成功！");
    }

    //删除学生方法
    public final void deleteStudentById() {
        //调用底层方法，判断所删除ID是否存在
        String deleteId = studentInfo();
        //调用底层方法，根据所给ID对学生进行删除
        studentService.deleteStudentInfo(deleteId);
        System.out.println("删除成功！");
    }

    //查询学生方法
    public final void findAllStudent() {
        //先拿到装有所有Student对象的数组
        Student[] students = studentService.findAllStudent();
        //判断数组是否为空
        if (students == null) {
            System.out.println("查无信息，请重新输入！");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        //循环遍历打印数组中所有元素
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            //判断是否有null，打印不为null的元素
            if (student != null) {
                System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "岁" + "\t" + student.getBirthday());
            }
        }
    }

    //添加学生方法
    public final void addStudent() {
        String id;
        //死循环判断学生ID是否存在
        while (true) {
            System.out.println("请输入学生ID：");
            id = sc.next();
            boolean extist = studentService.isExists(id);
            if (extist) {
                System.out.println("该学生ID已被占用，请重新输入！");
            } else {
                break;
            }
        }

        //对学生对象进行封装
        Student student = inputStudentInfo(id);

        //调用底层添加学生方法
        boolean flag = studentService.addStudent(student);
        //根据返回值判断是否添加成功
        if (flag) {
            System.out.println("添加成功！");
        } else {
            System.out.println("添加失败！");
        }
    }

    //调用底层方法，判断ID是否存在数据库中
    public final String inputStudentId() {
        String id;
        while (true) {
            System.out.println("请输入学生ID：");
            id = sc.next();
            boolean exists = studentService.isExists(id);
            if (!exists) {
                System.out.println("您输入的信息不存在，请重新输入！");
            } else {
                break;
            }
        }
        return id;
    }

    //调用底层方法，判断输入ID是否存在于数组中
    public String studentInfo(){
        String id;
        while (true){
            System.out.println("请输入学生ID：");
            id = sc.next();
            boolean exists = studentService.isExists(id);
            if (!exists){
                System.out.println("您输入的ID不存在，请重新输入！");
            }else {
                break;
            }
        }
        return id;
    }

    //将学生对象进行封装
    public abstract Student inputStudentInfo(String id);

}

